package com.bawei.lib_storage.mmkv.utils

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class MMKVProperty<T>(private  val defValue:T): ReadWriteProperty<MMKVOwner, T> {
    override fun getValue(thisRef: MMKVOwner, property: KProperty<*>): T {
//        when(defValue){
//            is String -> return thisRef.mmkv.getString(property.name,defValue) as T
//        }
        return thisRef.mmkv.run {
            when(defValue){
                is String -> getString(property.name,defValue)
                is Boolean -> getBoolean(property.name, defValue)
                is Long -> getLong(property.name, defValue)
                is Int -> getInt(property.name, defValue)
                is Float -> getFloat(property.name, defValue)
                is Double -> decodeDouble(property.name, defValue)
                else -> Unit
            }
        } as T

    }

    override fun setValue(thisRef: MMKVOwner, property: KProperty<*>, value: T) {
        thisRef.mmkv.run {
            when (value) {
                is String -> putString(property.name, value)
                is Boolean -> putBoolean(property.name, value)
                is Long -> putLong(property.name, value)
                is Int -> putInt(property.name, value)
                is Float -> putFloat(property.name, value)
                is Double -> encode(property.name, value)
                else -> Unit
            }
        }
    }
}



class MMKVNullableProperty<T>(private val clazz: Class<T>? = null, private val defValue: T? = null) :
    ReadWriteProperty<MMKVOwner, T?> {

    override fun getValue(thisRef: MMKVOwner, property: KProperty<*>): T? {
        return thisRef.mmkv.run {
            when (defValue) {
                is String -> getString(property.name, defValue)
                is Boolean -> getBoolean(property.name, defValue)
                is Long -> getLong(property.name, defValue)
                is Int -> getInt(property.name, defValue)
                is Float -> getFloat(property.name, defValue)
                is Double -> decodeDouble(property.name, defValue)
                else -> {
                    if (clazz == null) return null
                    val json = getString(property.name, defValue?.let { GSON.toJson(it) }) ?: "" //json串
                    //json字符串转对象
                    GSON.formJson(json, clazz)
                }
            }
        } as? T?
    }

    override fun setValue(thisRef: MMKVOwner, property: KProperty<*>, value: T?) {
        thisRef.mmkv.run {
            when (value) {
                is String -> putString(property.name, value)
                is Boolean -> putBoolean(property.name, value)
                is Long -> putLong(property.name, value)
                is Int -> putInt(property.name, value)
                is Float -> putFloat(property.name, value)
                is Double -> encode(property.name, value)
                else -> {
                    putString(property.name, GSON.toJson(value)) //对象转字符串
                }
            }
        }
    }

}